package ZhuangXiang;

public class Instanceof {
    public static void main(String[] args) {
        //向下转型前,应判断引用中的对象真实类型,保证类型转换的正确性
        //instanceof关键字
       Vetch vech = new Vetch();
//        vech.name = "jac";
      Car car = new Car();
        car.version = "Jack";
/       Bike bike = new Bike();
//        bike.color = "白色";
        //  Vetch ve = new Bike();\
//        Vetch ve = new Car();
//        Per per = new Per();
//        per.name = "hello";
//        Vetch vetch = null;
//        vetch  = bike;
//        per.goHome(car);
        //拆箱:将父类引用的真实子类对象,返回其本身类型
        //父类引用 Instanecof 类型  //bik
       if(vech instanceof Car){//判断引用中的真实子类对象是否为Car对象
        Car mycar =(Car)vech;
           System.out.println(mycar.version);
    }else( vech instanceof  Bike){
    Bike mybike =(Bike)vech;
            System.out.println(mybike.color);
    }
}

class Per {
    String name;

    public void goHome(Vetch ve) {
        System.out.print(name + "回家");
        ve.go();
    }
}

static class Vetch {
    String name;

    public void go() {
        System.out.print("启动汽车类");
    }
}

static class Car extends Vetch {
    String version;
    public void go() {
        System.out.print("开启汽车");
    }
}

static class Bike extends Vetch {
    String color;

    public void go() {
        System.out.print("开启自行车");
    }
}